package cn.liuyiyou.lean.es.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;

/**
 * TODO
 *
 * @author liuyiyou
 * @version 1.0
 * @date 2021/7/6 9:19
 */
@Slf4j
public abstract class JacksonUtils {

    static final ObjectMapper objectMapper = new ObjectMapper();

    public static <T> T parse(String s, Class<T> tClass) {
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        T t = null;
        try {
            t = objectMapper.readValue(s, tClass);
        } catch (JsonProcessingException e) {
            log.error("类型转换异常", e);
        }
        return t;
    }


    public static String toJsonStr(Object object) {
        String s = null;
        try {
            s = objectMapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            log.error("类型转换异常", e);
        }
        return s;
    }
}
